<?php
namespace app\controllers;
use app\models\Menu;
use yii\db\Query;

class IndexController extends AdminBaseController {



    public function actionIndex(){
        $identity=\Yii::$app->user->identity;
        if (!is_null($identity)){
         $role_id=\Yii::$app->user->identity->role_id;
         $menu_id=(new Query())->from('yii_role')->select('authority')->where(array('id'=>$role_id))->one();
         $menu=Menu::find()->where(['in','id',json_decode($menu_id['authority'],true)])->orderBy('sort asc')->asArray()->all();
         foreach ($menu as $k=>$v){
            $menu[$k]['son']=$this->GetMenu($v['id']);
            foreach ($menu[$k]['son'] as $k2=>$v2){
                $menu[$k]['son'][$k2]['son']=$this->GetMenu($v2['id']);
             }
         }
         $name=(new Query())->from('yii_wxapp')->one();
        return $this->render('index',['menu'=>$menu,'name'=>$name]);
        }
    }


    //获取子菜单
    public function GetMenu($id){
        $list=Menu::find()->where(['parent_id'=>$id])->orderBy('sort asc')->asArray()->all();
        return $list;
    }

    //后台首页
    public function actionMain(){
         //今天投递人数
         $time=mktime(0,0,0,date('m'),date('d'),date('Y'));
         $worktoday=(new Query())->select('*')->from('yii_user_work')->where(['>=','create_time',$time])->count();
        //会员总人数
         $user=(new Query())->select('*')->from('yii_user')->count();
        //公司总人数
         $company=(new Query())->select('*')->from('yii_company')->count();
         //文章总数
         $article=(new Query())->select('*')->from('yii_article')->count();
        //职位总数
         $work=(new Query())->select('*')->from('yii_work')->count();
         //本日注册会员
         $userday=$this->GetUserByTime(1);
        //本周注册会员
         $userweek=$this->GetUserByTime(2);
         //本月注册会员
         $usermonth=$this->GetUserByTime(3);
         $companyweek=$this->GetCompanyByTime(1);
         $companymonth=$this->GetCompanyByTime(2);

        return $this->render('main',['user'=>$user,'worktoday'=>$worktoday,
            'company'=>$company,'work'=>$work,'article'=>$article,
            'companyweek'=>$companyweek,'companymonth'=>$companymonth,
            'userday'=>$userday,'userweek'=>$userweek,'usermonth'=>$usermonth]);
    }

    //通过时间查询用户
    public function GetUserByTime($type){
        switch ($type){
            case 1:
                $time=mktime(0,0,0,date('m'),date('d'),date('Y'));
                $list=(new Query())->select('user_id')->from('yii_user')->where(['>=','create_time',$time])->count();
                break;
            case 2:
                $time=mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"));
                $list=(new Query())->select('user_id')->from('yii_user')->where(['>=','create_time',$time])->count();
                break;
            case 3:
                $time=mktime(0,0,0,date('m'),1,date('Y'));
                $list=(new Query())->select('user_id')->from('yii_user')->where(['>=','create_time',$time])->count();
                break;
        }
        return $list;
    }

    //通过时间查询公司
    public function GetCompanyByTime($type){
        switch ($type){
            case 1:
                $time=mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"));
                $list=(new Query())->select('*')->from('yii_company')->where(['>=','create_time',$time])->count();
                break;
            case 2:
                $time=mktime(0,0,0,date('m'),1,date('Y'));
                $list=(new Query())->select('*')->from('yii_company')->where(['>=','create_time',$time])->count();
                break;
        }
        return $list;
    }

    //注册量
    public function actionRegister(){
//当前日期
$sdefaultDate = date("Y-m-d");
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$first=1;
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$w=date('w',strtotime($sdefaultDate));
//获取本周开始日期，如果$w是0，则表示周日，减去 6 天
$week_start=strtotime(date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days')));

        $date=[];
        for($i=0;$i<7;$i++){
            array_push($date,$week_start+$i*86400);//每隔一天赋值给数组
        }

        //多加一天
        array_push($date,$week_start+7*86400);

        
        $data=[];
        foreach($date as $k=>$v){
            if (($k+1)<count($date)){
            	$count=(new Query)->select('*')->from('yii_user')->where(['between','create_time',$date[$k],$date[$k+1]])->count();
                array_push($data,$count);
            }
        }
    	
    	
    	 $data2=[];
        foreach($date as $k=>$v){
            if (($k+1)<count($date)){
            	$count=(new Query)->select('*')->from('yii_company')->where(['between','create_time',$date[$k],$date[$k+1]])->count();
                array_push($data2,$count);
            }
        }
    	
    	

        return $this->render('register',['data'=>$data,'data2'=>$data2]);
    }

    //活跃度
    public function actionActivity(){
    	$firstday = strtotime(date('Y-m-01', strtotime(date("Y-m-d"))));//这个月第一天的时间戳
        $day=[];//天数
        $date=[];
        for($i=0;$i<date('t');$i++){
            array_push($day,date('d',$firstday+$i*86400));//每隔一天赋值给数组
            array_push($date,$firstday+$i*86400);//每隔一天赋值给数组
        }
           //多加一天
        array_push($date,$date[count($date)-1]+86400);
        
             //订单情况
        $data=[];
        foreach($date as $k=>$v){
            if (($k+1)<count($date)){
            	$count=(new Query)->select('*')->from('yii_work')->where(['between','create_time',$date[$k],$date[$k+1]])->count();
                array_push($data,$count);
            }
        }


        
        return $this->render('activity',['day'=>$day,'data'=>$data]);
    }
  
  public function actionMap(){
  	
  	     return $this->render('map');
  }


}
